package com.huizu.pay.util;

import java.math.BigDecimal;
import java.util.*;

/**
 * String转String[]
 * @author 
 */
public class StringSplit {

	public static List<String> stringToList(String strs) {
		return StringUtil.isNotBlank(strs) ? Arrays.asList(strs.split(",")) : new ArrayList<>();
	}

	public static List<String> stringToList2(String strs) {
		List<String> strings = new ArrayList<>();
		if (StringUtil.isNotBlank(strs)) {
			strs = strs.replace("[", "").replace("]", "").replace("\'", "");
			strings = Arrays.asList(strs.split(","));
		}
		return strings;
	}

	public static List<Integer> stringToIntegers(String strs) {
		List<Integer> integers = new ArrayList<>();
		if (StringUtil.isNotBlank(strs)) {
			String[] strings = strs.split(",");
			for (int i = 0; i < strings.length; i++)
				integers.add(Integer.parseInt(strings[i]));
		}
		return integers;
	}

	public static String[] stringToSet(String strs) {
		return StringUtil.isNotBlank(strs) ? strs.split(",") : new String[]{};
	}

	public static List<String> stringBuilderToList(StringBuilder stringBuilder) {
		return stringBuilder != null ? Arrays.asList(stringBuilder.toString().split(",")) : new ArrayList<>();
	}

	public static String listToString(List<?> list) {
		String string = "";
		if (list != null)
			string = list.toString().replace("[", "").replace("]", "").replace(" ", "");
		return string;
	}

	public static String listToString2(List<?> list) {
		String string = "";
		if (list != null)
			string = list.toString().replace("[", "").replace("]", "").replace(",", "/").replace(" ", "");
		return string;
	}

	public static List<Map<String, String>> stringToListMap(String strs){
		int count = 0;
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		List<String> listString = new ArrayList<String>();
		String string = strs.replace("[", "").replace("]", "").replace("\"", "");
		int index = string.indexOf("},");
		while (index != -1) {
			listString.add(count, string.substring(0, index+1));
			string = string.substring(index+2, string.length());
			index = string.indexOf("},");
			count += 1;
		}
		listString.add(count, string);
		for (String str : listString) {
			Map<String, String> map = new HashMap<String, String>();
			String s[] = str.replace("{", "").replace("}", "").split(",");
			for (int i=0; i<s.length; i++) {
				String strMap[] = s[i].split(":");
				String m = strMap.length > 1 ? strMap[1] : "";
				map.put(strMap[0], m);
			}
			list.add(map);
		}
		return list;
	}

	public static String objectToString(Object object) {
		return object != null ? String.valueOf(object) : "";
	}

	public static int objectToInteger(Object object) {
		return object != null ? Integer.parseInt(String.valueOf(object)) : 0;
	}

	public static long objectToLong(Object object) {
		return object != null ? Long.parseLong(String.valueOf(object)) : 0;
	}

	public static double objectToDouble(Object object) {
		return object != null ? Double.parseDouble(String.valueOf(object)) : 0.0;
	}

	public static BigDecimal objectToBigDecimal(Object object) {
		return object != null ? new BigDecimal(String.valueOf(object)) : BigDecimal.ZERO;
	}

	/**
	 * 计算第一个集合比第二个集合缺少的数据
	 * @param oldList
	 * @param newList
     * @return
     */
	public static List<String> listByMiss(List<String> oldList, List<String> newList) {
		List<String> removes = new ArrayList<>();
		if (oldList != null && oldList.size() > 0 && newList != null) {
			for (String oldId : oldList) {
				boolean removeBoo = true;
				for (String newId : newList) {
					if (oldId.equals(newId)) {
						removeBoo = false;
						break;
					}}
				if (removeBoo)
					removes.add(oldId);
			}}
		return removes;
	}

	/**
	 * 判断俩个集合中相同的元素
	 * @param oldList
	 * @param newList
     * @return
     */
	public static List<String> listByEquals(List<String> oldList, List<String> newList) {
		List<String> equals = new ArrayList<>();
		if (oldList != null && oldList.size() > 0 && newList != null) {
			for (String oldId : oldList) {
				for (String newId : newList) {
					if (oldId.equals(newId)) {
						equals.add(oldId);
						break;
					}}}}
		return equals;
	}

	/**
	 * 计算页数
	 * @param page
	 * @param limit
     * @return
     */
	public static int pagination(int page, int limit) {
		int pagin = 0;
		if (page != 1)
			pagin = (page - 1) * limit;
		return pagin;
	}

	public static String getImageUrl(String image, String type) {
		String imageUrl = "";// 图片路径
		if (StringUtil.isNotBlank(image) && StringUtil.isNotBlank(type))
			imageUrl = "/pos/mappingImg/".concat(image.substring(image.indexOf(type)));
		return imageUrl;
	}

}
